package com.example.goodog;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.os.AsyncTask;

import com.example.goodog.interfaces.LoggedinCallback;
import com.example.goodog.items.UserItem;
import com.google.gson.Gson;


public class LoginManager {
	
	public LoggedinCallback callback;

	
	private final String NAMESPACE = "http://GooDog_Results.org/";
	private final String URL = "http://goodog.zapto.org/WebService1.asmx";
	private final String METHOD_NAME = "Login";


	// flag for GPS status
	private String Mail;
	private String Password;
	private UserItem user;
	

	public LoginManager(String Mail, String Password) {
		this.Mail = Mail;
		this.Password = Password;
		this.user = null;
	}

	public LoginManager() {
		this(null,null);
	}

	public void StartLogingIn() {
		
		if(Mail == null || Password == null)
		{
			callback.AfterTheAttempt();
			return;
		}
			
		UserLoginTask mAuthTask = new UserLoginTask();
		mAuthTask.execute(Mail,Password);
			
	}

	public String getMail() {
		return Mail;
	}

	public void setMail(String mail) {
		Mail = mail;
	}

	public String getPassword() {
		return Password;
	}

	public void setPassword(String password) {
		Password = password;
	}
	
	
	
	public class UserLoginTask extends AsyncTask<String, Void, Boolean> {
		@Override
		protected Boolean doInBackground(String... params) {

			String LoginResult = Login(params[0],params[1]);
			
			if(!LoginResult.equals("BAD"))
			{
				
				Gson gson = new Gson(); 
				LoginResponse lr = gson.fromJson(LoginResult, LoginResponse.class);
				lr.User.Dogs = lr.Dogs;
				for (int i=0 ; i<lr.Images.size() ; i++) 
				{
					lr.User.Dogs.get(i).ImageBlob = lr.Images.get(i);
				}
				setUser(lr.User);
				callback.loggedInCall();
				callback.AfterTheAttempt();
				return true;
			}
			callback.AfterTheAttempt();
			return false;
			
		}

		@Override
		protected void onPreExecute() {

		}

		@Override
		protected void onProgressUpdate(Void... values) {
		}
	}
	
	private String Login(String Mail, String Password) {
	    //Create request
	    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
	    
	    request.addProperty("Mail",Mail);
	    request.addProperty("Password",Password);
	    
	    //Create envelope
	    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
	            SoapEnvelope.VER11);
	    envelope.dotNet = true;
	    //Set output SOAP object
	    envelope.setOutputSoapObject(request);
	    //Create HTTP call object
	    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
	 
	    try 
	    {	     
	        androidHttpTransport.call(NAMESPACE+METHOD_NAME, envelope);

            // Get the response
            SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
            // Assign it to static variable
            return response.toString(); 
            
	    } 
	    catch (Exception e) 
	    {
	        e.printStackTrace();
	    }
	    return "BAD"; 
	}

	public UserItem getUser() {
		return user;
	}

	public void setUser(UserItem user) {
		this.user = user;
	}

	public void LogOut() {
		Mail = null;
		Password = null;
		user = null;
		callback.loggedOutCall();
		callback.AfterTheAttempt();
		
	}


}